#!/bin/bash

# Move new files into place if no current
DIR="/usr/local/etc/foundationdb/sql"
for f in "${DIR}"/*.new; do
    WITHOUT_NEW="${f%.new}"
    if [ ! -f "${WITHOUT_NEW}" ]; then
        mv "${f}" "${WITHOUT_NEW}"
    fi
done

# remove .new file if old and new are equal
for file in "${DIR}"/*.new; do
    WITHOUT_NEW="${file%.new}"
    diff ${file} ${WITHOUT_NEW} > /dev/null
    if [ $? -eq 0 ]; then
       rm ${file}
    fi
done 

/bin/launchctl load -w /Library/LaunchDaemons/com.foundationdb.layer.sql.plist

exit 0
